﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DomainModel;
using Utils;
using WebUI.Utils;

namespace WebUI.Models
{
    public class CoSavedJobViewModel: BaseViewModel
    {
        private int _numPerPage = 15;

        public CoSavedJobViewModel()
            : base()
        { }

        public CoSavedJobViewModel(int page)
            : base(new { Page = page})
        {

        }

        public string AvatarUrl { get; set; }
        public Co Co { get; set; }

        public int PreviousPage { get; set; }
        public int NextPage { get; set; }

        //ind list
        public List<CoJob> CoJobList { get; set; }

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            int page = dynParams.Page;

            //get co
            Co = DataUtils.GetOne<Co>(ConstUtils.SP_GetCo, new { Identity.LoginID, CoID = Identity.AccountID });
            //get avatar 
            AvatarUrl = ImageUtils.GetAvatarRelativeUrl(AvatarType.CoAvatar, Co.CoID, AvatarSizeEnum.Avatar_80x80, Co.AvatarFileExtension);

            //init coJobList
            CoJobList = new List<CoJob>();
            //get joblist
            var jobList = DataUtils.GetList<Job>(ConstUtils.SP_GetSavedJob, new { Identity.LoginID, page, NumPerPage = _numPerPage });
            if (jobList.Count > 0)
            {
                //get co list
                var coIDs = string.Join(",", jobList.Select(x => x.CoID));
                var coList = DataUtils.GetList<Co>(ConstUtils.SP_GetCoListByIDs, new { CoIDs = coIDs });
                
                foreach (var job in jobList)
                {
                    var coJob = new CoJob();
                    coJob.Job = job;
                    coJob.Co = coList.Find(x => x.CoID == job.CoID);
                    coJob.Co["AvatarUrl"] = ImageUtils.GetAvatarRelativeUrl(AvatarType.CoAvatar, coJob.Co.CoID, AvatarSizeEnum.Avatar_80x80, coJob.Co.AvatarFileExtension);
                    CoJobList.Add(coJob);
                }
            }
            //get next page
            int totalCount = jobList != null && jobList.Count > 0 ? jobList[0].TotalCount : 0;
            PreviousPage = CommonUtilities.GetPreviousPage(page);
            NextPage = CommonUtilities.GetNextPage(totalCount, page, _numPerPage);
        }
    }
}